package cn.chencq.proxy.timer.handler.custom;

import java.net.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.chencq.proxy.timer.handler.GeneralProxyHandler;
/**
 * @website http://www.proxy360.cn
 * @author chencq
 * @desc proxy360
 * 
 */
public class ProxyHandlerByP360 extends GeneralProxyHandler {

	@Override
	public String getHandler() {

		return "PROXY360";
	}

	@Override
	public int parseResp(String rid,Document doc) {
		Set<String> proxySet = new HashSet<String>();
		Elements divs = doc.getElementsByClass("proxylistitem");
		System.out.println(divs.size());
		for (Element div : divs) {
			Elements spans = div.getElementsByTag("span");
			String ip = spans.get(0).html().replaceAll(" ", "");
			String port = spans.get(1).html().replaceAll(" ", "");
			proxySet.add(ip + ":" + port);
		}
		super.putToCache(rid,proxySet);
		return proxySet.size();
	}
	
	@Override
	public int crawler(String rid,String url,Proxy proxy) {
		int ret = 0;
		try {
			Document doc = requestDoc(rid,url,proxy);
			if (doc != null) {
				ret = parseResp(rid,doc);
			} else {
				ret = 0;
			}
		} catch (Exception e) {
			ret = 0;
		}
		return ret;
	}

}
